﻿//=======================================================
// Author: Jonas Follesø <jonas@follesoe.no>
// Copyright (c) 2008
//
// Licensed under the Microsoft Public License (Ms-PL)
// A copy of the license is available at 
// http://www.opensource.org/licenses/ms-pl.html
//=======================================================

namespace Colorful.Blend.AddIn
{
    using System.ComponentModel;
    using System.ComponentModel.Composition;
    using Colorful.Control;
    using Colorful.Control.Services;
    using Microsoft.Expression.Framework;
    using Microsoft.Expression.Framework.UserInterface;

    [Export(typeof (IPackage))]
    public class ColorCommunityAddIn : IPackage //CommandTarget
    {
        private SearchPanel colorPanel;
        private ILoggerService messageLoggingService;
        private ColorfulViewModel viewModel;
        private IWindowService windowService;

        #region IPackage Members

        public void Load(IServices services)
        {
            windowService = services.GetService<IWindowService>();
            messageLoggingService = services.GetService<ILoggerService>();
            Initialize();
        }

        public void Unload()
        {
        }

        #endregion

        public void Initialize()
        {
            viewModel = new ColorfulViewModel(new AdobeKuler(), new CfXamlClipboardFormatter(), new XamlTextClipboardFormatter());
            viewModel.PropertyChanged += OnViewModelPropertyChanged;
            colorPanel = new SearchPanel(viewModel, true);
            if (windowService.PaletteRegistry["ColorPanel"] == null)
                windowService.RegisterPalette("ColorPanel", colorPanel, "Colorful Expression - Get colors online");
        }

        private void OnViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ErrorMessage" && viewModel.ErrorMessage != string.Empty)
            {
                messageLoggingService.ActiveLogger.WriteLine("An Exception occurred in the Colorful Expression AddIn");
                messageLoggingService.ActiveLogger.WriteLine(viewModel.ErrorMessage);
            }
        }
    }
}